package com.example.jl.bethinner.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.jl.bethinner.R;
import com.example.jl.bethinner.adapter.HELCityChooserAdapter;
import com.example.jl.bethinner.adapter.HELProvinceChooserAdapter;
import com.example.jl.bethinner.base.BaseActivity;
import com.example.jl.bethinner.domain.BFNewModel;
import com.example.jl.bethinner.domain.City;
import com.example.jl.bethinner.domain.Province;
import com.google.gson.reflect.TypeToken;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import java.util.ArrayList;
import java.util.List;

public class HorizontalExpansionActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private ListView base_listview;
    private ListView data_listview;
    private List<Province> provinces;
    private HELProvinceChooserAdapter provinceAdapter = null;
    private List<City> cities;
    private HELCityChooserAdapter cityAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horizontal_expansion);
        provinces = new ArrayList<>();
        cities = new ArrayList<>();
        Province whole_nation = new Province();
        whole_nation.setProvince("全国");
        whole_nation.setProvinceid("-1");
        provinces.add(whole_nation);
        base_listview = (ListView) findViewById(R.id.base_listview);
        data_listview = (ListView) findViewById(R.id.data_listview);
        setProvince();
        base_listview.setOnItemClickListener(this);
        data_listview.setOnItemClickListener(this);
    }

    public void setProvince() {

        Ion.with(this).load("GET", "http://zxm.buildface.com/api.php/qibo/region/allprovinces")
                .as(new TypeToken<BFNewModel<List<Province>>>() {
                }).setCallback(new FutureCallback<BFNewModel<List<Province>>>() {
            @Override
            public void onCompleted(Exception e, BFNewModel<List<Province>> result) {
                if (null == e) {
                    if (result.getStatus().equals("success")) {
                        provinces.addAll(result.getLists());
                        provinceAdapter = new HELProvinceChooserAdapter(HorizontalExpansionActivity.this, provinces);
                        base_listview.setAdapter(provinceAdapter);
                        setCity(provinces.get(0).getProvinceid());
                    } else
                        Toast.makeText(HorizontalExpansionActivity.this, result.getStatus(), Toast.LENGTH_SHORT).show();
                } else {
                    e.printStackTrace();
                    Toast.makeText(HorizontalExpansionActivity.this, "省数据请求失败", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    public void setCity(String selectProvinceid) {
        if ("-1".equals(selectProvinceid)) {
            City whole_city = new City();
            whole_city.setCity("全国");
            whole_city.setCityid("-1");
            cities.clear();
            cities.add(whole_city);
            cityAdapter = new HELCityChooserAdapter(this, cities);
//            cityAdapter = new QuickAdapter<City>(HorizontalExpansionActivity.this, android.R.layout.simple_list_item_1) {
//                @Override
//                protected void convert(BaseAdapterHelper helper, City item) {
//                    helper.setText(android.R.id.text1, item.getCity());
//                }
//            };
            data_listview.setAdapter(cityAdapter);
//            cityAdapter.addAll(cities);
        } else {
            Ion.with(this).load("http://zxm.buildface.com/api.php/qibo/region/allcities")
                    .setBodyParameter("province_id", selectProvinceid)
                    .as(new TypeToken<BFNewModel<List<City>>>() {
                    }).setCallback(new FutureCallback<BFNewModel<List<City>>>() {
                @Override
                public void onCompleted(Exception e, BFNewModel<List<City>> result) {
                    if (null == e) {
                        if (result.getStatus().equals("success")) {
                            cities = result.getLists();
                            cityAdapter = new HELCityChooserAdapter(HorizontalExpansionActivity.this, cities);
//                            cityAdapter = new QuickAdapter<City>(HorizontalExpansionActivity.this, android.R.layout.simple_list_item_1) {
//                                @Override
//                                protected void convert(BaseAdapterHelper helper, City item) {
//                                    helper.setText(android.R.id.text1, item.getCity());
//                                }
//                            };
                            data_listview.setAdapter(cityAdapter);
//                            cityAdapter.addAll(cities);
                        } else
                            Toast.makeText(HorizontalExpansionActivity.this, result.getStatus(), Toast.LENGTH_SHORT).show();
                    } else {
                        e.printStackTrace();
                        Toast.makeText(HorizontalExpansionActivity.this, "市数据请求失败", Toast.LENGTH_SHORT).show();
                    }

                }
            });
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.base_listview:
                setCity(provinces.get(position).getProvinceid());
                provinceAdapter.setPositionId(position);
                break;
            case R.id.data_listview:
                Toast.makeText(HorizontalExpansionActivity.this, cities.get(position).getCity(), Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
